<template>
	<el-form size="large" ref="formRef" :rules="formRules" :model="formData" class="login-content-form">
		<el-form-item class="login-animation1" prop="Phone">
			<el-input text placeholder="请输入手机号" v-model="formData.Phone" clearable autocomplete="off">
				<template #prefix>
					<i class="iconfont icon-dianhua el-input__icon"></i>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item class="login-animation2" prop="Captcha">
			<el-col :span="15">
				<el-input text maxlength="4" placeholder="请输入验证码" v-model="formData.Captcha" clearable
					autocomplete="off">
					<template #prefix>
						<el-icon class="el-input__icon"><ele-Position /></el-icon>
					</template>
				</el-input>
			</el-col>
			<el-col :span="1"></el-col>
			<el-col :span="8">
				<el-button v-waves class="login-content-code" @click="loadCaptcha">获取验证码</el-button>
			</el-col>
		</el-form-item>
		<el-form-item class="login-animation3">
			<el-button type="primary" class="login-content-submit" round v-waves @click="onLogin">
				<span>登 录</span>
			</el-button>
		</el-form-item>
		<div class="font12 mt30 login-animation4 login-msg">* 温馨提示：123123123123123123</div>
	</el-form>
</template>

<script setup lang="ts" name="loginMobile">
import { ref } from 'vue';
import EMsg from '/@/common/eMsg';
import { Post, PostAsync } from '/@/common/http';
import { useRoute, useRouter } from 'vue-router';
import { initRoutes } from '/@/router/router';
import { Session } from '/@/utils/storage';
import { formatAxis } from '/@/utils/formatTime';

const route = useRoute();
const router = useRouter();

const formRef = ref();
const formRules = ref({
	Phone: [{ required: true, message: '手机号码不可为空', trigger: 'blur' }],
	Captcha: [{ required: true, message: '验证码不可为空', trigger: 'blur' }],
});
// 定义变量内容
const formData = ref({
	Phone: '',
	Captcha: '',
});

const loadCaptcha = () => {
	if (formData.value.Phone == '') {
		return EMsg.error('请输入手机号码。');
	}
	Post('Login/SendSms', formData.value, (res: any) => {
		EMsg.success(res.Data);
	});
};

const onLogin = () => {
	formRef.value.validate((valid: any) => {
		if (valid) {

			var DeviceId = localStorage.getItem(formData.value.Phone);
			if (DeviceId == null) {
				DeviceId = GetDeviceId();
				localStorage.setItem(formData.value.Phone, DeviceId);
			}
			const data = {
				Phone: formData.value.Phone,
				Captcha: formData.value.Captcha,
			};
			PostAsync('Login/LoginPhone', data).then(async (res: any) => {
				if (res.Code == 0) {
					Session.set('UserToken', res.Data);
					const isNoPower = await initRoutes();
					signInSuccess(isNoPower);
				} else {
					EMsg.error(res.Message);
				}
			});
		}
	});
};

const GetDeviceId = () => {
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
		var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
		return v.toString(16);
	});
}
// 登录成功后的跳转
const signInSuccess = (isNoPower: boolean | undefined) => {
	if (isNoPower) {
		EMsg.warning('抱歉，您没有登录权限');
		Session.clear();
	} else {
		let currentTimeInfo = formatAxis(new Date());
		if (route.query?.redirect) {
			router.push({
				path: <string>route.query?.redirect,
				query: Object.keys(<string>route.query?.params).length > 0 ? JSON.parse(<string>route.query?.params) : '',
			});
		} else {
			router.push('/');
		}
		// 登录成功提示
		EMsg.success(`${currentTimeInfo}，欢迎登录李狗蛋的管理系统`);
	}
};


</script>

<style scoped lang="scss">
.login-content-form {
	margin-top: 20px;

	@for $i from 1 through 4 {
		.login-animation#{$i} {
			opacity: 0;
			animation-name: error-num;
			animation-duration: 0.5s;
			animation-fill-mode: forwards;
			animation-delay: calc($i/10) + s;
		}
	}

	.login-content-code {
		width: 100%;
		padding: 0;
	}

	.login-content-submit {
		width: 100%;
		letter-spacing: 2px;
		font-weight: 300;
		margin-top: 15px;
	}

	.login-msg {
		color: var(--el-text-color-placeholder);
	}
}
</style>
